import {defineStore} from 'pinia'
import { RoleApi } from "@/api/role/index.js";

export const useRoleStore = defineStore('role',{
    state:()=>({
        roleList:[],
        queryList:{
            page:1,
            size:10,
            total:0,
            totalPage:0,
            name: "",
            code: "",
        },
        roleDetail:{},
    }),
    actions:{
        async createRole(data){
            await RoleApi.createRole(data)
            this.getRoleList()
        },
        async deleteRole(id){
            await RoleApi.deleteRole(id)
            this.getRoleList()
        },
        async getRoleDetail(id){
            const res =await RoleApi.getRoleDetail(id)
            this.roleDetail = res.data
        },
        async getRoleList(){
            const res = await RoleApi.getRoleList(this.queryList)
            this.roleList = res.data.records
            this.queryList.page = res.data.pageNumber
            this.queryList.size = res.data.pageSize
            this.queryList.total = res.data.totalRow
            this.queryList.totalPage = res.data.totalPage
        },
        async updateRole(data){
            await RoleApi.updateRole(data)
            this.getRoleList()
        }
    }
})